home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / AutoPay SDK Mac R1.02 / AutoPay™ Development / how to call AutoPay in your app / how to call AutoPay.c next >
Encoding:
C/C++ Source or Header  |  1996-04-20  |  10.8 KB  |  334 lines  |  [TEXT/MMCC]

  1. /*
  2.         File: "how to call AutoPay.c"
  3.  
  4.         Last updated 4/20/96
  5.  
  6.         This file implements a simple application demonstrating how to use
  7.         Release Software's AutoPay™ code resource in your own application.
  8.  
  9.         This source code should run unmodified on both MetroWerks CodeWarrior C/C++
  10.         and Symantec Think C compilers. Pascal development environments are also
  11.         supported in supplementary files.
  12.  
  13.         What is this?:    This is a meaningless app that puts up a window, waits
  14.                         for you to press a button, then launches the AutoPay
  15.                         module. When the AutoPay module is finished running,
  16.                         this mini-app puts up a final window. To actually
  17.                         run this, you'll need to include the AutoPay.rsrc
  18.                         file in your project, along with the minimal
  19.                         resource set this mini-app uses. We've thoughtfully
  20.                         included those resources in the file "how to call.π.rsrc".
  21.  
  22.         To build this project:    To build this project, include the "how to call.π.rsrc",
  23.                                 the AutoPay.rsrc file, an ANSI C library
  24.                                 (2-byte integers; in MetroWerks look for ANSI (2i/f) C.A5), 
  25.                                 and this source code file.
  26.         
  27.         Tech Support:    If you're a developer and you need technical support, we'll
  28.                         be glad to help. We also welcome your comments, suggestions,
  29.                         and gripes.
  30.  
  31.         Technical support:     (415) 833-0200
  32.                         matthew@releasesoft.com
  33.                             
  34.  
  35.         Sales and info:        (800) 210-5517
  36.                         info@releasesoft.com
  37. */
  38.  
  39. #include "DMIdeveloper.h.c"
  40. #include <string.h>
  41.  
  42. typedef pascal long (*AutoPayCodePtr) (long);
  43.  
  44. void ToolBoxInit (void);
  45. void InitAutoPay(digiMonBlock *myDMCB);
  46. void DoAutoPay(digiMonBlock *dmiBlock);
  47.  
  48. digiMonBlock    gDMCB;
  49.  
  50. /*
  51.         Function: main
  52.  
  53.         Calls InitAutoPay() to fill in the struct that we pass to the AutoPay
  54.         code resource. Calls DoAutoPay to execute the resource.
  55.  
  56.         Returns: Nothing.
  57. */
  58.  
  59. void main (void)
  60. {
  61.     
  62.     // Set the app up
  63.     ToolBoxInit();
  64.     ParamText("\pPress OK to launch the AutoPay code resource.", "\p", "\p", "\p");
  65.     NoteAlert(129, nil);
  66.  
  67.     
  68.     
  69.     // Fill in the AutoPay Control Block
  70.     InitAutoPay(&gDMCB);
  71.  
  72.     // Execute the code resource using our DMCB
  73.     DoAutoPay(&gDMCB);
  74.     
  75.     // Shut the app down
  76.     ParamText("\pCode resource finished executing.", "\p", "\p", "\p");
  77.     NoteAlert(129, nil);
  78.     ExitToShell();
  79. }
  80.  
  81. /*
  82.         Function: ToolBoxInit
  83.  
  84.         The usual suspects.
  85.  
  86.         Returns: Nothing.
  87. */
  88.  
  89. void    ToolBoxInit(void)
  90. {
  91.     InitGraf((Ptr) &qd.thePort);
  92.     InitFonts();
  93.     InitWindows();
  94.     InitMenus();
  95.     FlushEvents(everyEvent,0);
  96.     TEInit();
  97.     InitDialogs(0L);
  98.     InitCursor();
  99. }
  100.  
  101. /*
  102.         Function: InitAutoPay
  103.  
  104.         Fills in the digiMonBlock struct with info appropriate for our sample product.
  105.         Although this sample app doesn't do it, be sure to deallocate any memory allocated
  106.         by this function after your call to DoAutoPay().
  107.  
  108.         Returns: Filled in struct in the myDMCB parameter
  109. */
  110.  
  111. void InitAutoPay(digiMonBlock *myDMCB)
  112. {
  113.     short i;
  114.     
  115.     // General settings
  116.     
  117.     strcpy(myDMCB->programName, "Testbed 1.0");
  118.     myDMCB->returnSerialNum = 1;                         // 1=true; 0=false
  119.     myDMCB->areAdditionalItemsForSale = 1;                    // 1=true; 0=false
  120.     strcpy(myDMCB->programPassword, "tempPassword");
  121.     strcpy(myDMCB->programID, "123456");
  122.     strcpy(myDMCB->programSource, "CIS");
  123.     
  124.  
  125.     // Security
  126.     
  127.     // This is a secret string of ascii characters used for encryption.
  128.     // You should keep this key confidential, even among your own employees.
  129.     // The string is comprised of visible ASCII characters - i.e. don't
  130.     // use control characters. But punctuation is okay.
  131.     
  132.     strcpy(myDMCB->encodeMeth, "sampleEncodeUseYourOwn");
  133.  
  134.     // Screen Text
  135.     
  136.     // You can alter the text that appears on the top of each dialog box displayed
  137.     // by AutoPay. Normally, AutoPay reads "styled text" resources
  138.     // from your resource file, but if you want to change the text on the fly, so
  139.     // to speak, you can just pass a pointer leading to the appropriate
  140.     // text and style handle. Note that you must first initialize all 10 text pointers
  141.     // to zero so that AutoPay can tell the difference between a zero pointer
  142.     // (i.e. you don't want to replace the default text in the resource file) and a
  143.     // non-zero pointer (you *do* want to change the text). See below for an example.
  144.     
  145.     for (i=0; i<=9; i++) {
  146.         myDMCB->screenText[i] = 0;
  147.     }
  148.     
  149.     // Next be sure to allocate memory for any pointer that you want AutoPay
  150.     // to use. For instance, to change the text that appears on the opening screen,
  151.     // set the screenText[0] pointer, like this:
  152.     
  153.     
  154.     myDMCB->screenText[0] = NewPtr(200);
  155.     myDMCB->screenText[0] = "AutoPay is embedded in a test application.\rBelow, select how you want to pay.";
  156.     myDMCB->textSize[0] = 10;
  157.     myDMCB->textFont[0] = applFont;
  158.     
  159.     
  160.     // ...for a complete list of which array item corresponds to which dialog,
  161.     // see printed docs.
  162.     
  163.     // The users name
  164.     
  165.     // If you already know the users name for some reason, you should send it
  166.     // to the DM Module, so that DM doesn't have to ask the user for it. If you
  167.     // don't know it, then **be sure** to set the usersName string to null. In
  168.     // any case, if the user successfully completes enough of the DM transaction,
  169.     // his name will be returned to you in this string, should you want to use it,
  170.     // for registration splash screens or whatever.
  171.     
  172.     strcpy(myDMCB->usersName, "");     // I don't know it, so it to null!
  173.     
  174.  
  175.     // Software "switches"
  176.  
  177.     // AutoPay is continually being upgraded to add new features. You can turn new
  178.     // features on and off by using a series of software "switches", which are set
  179.     // through the "privateKey" field. The format of this field is: the asterisk
  180.     // character ('*') followed by a series of 1's and 0's representing whether a
  181.     // a switch (and thus a particular feature) is turned on or off. Currently you
  182.     // can access two software switches, but six more are slated for future use.
  183.     //
  184.     // The format is:
  185.     //
  186.     //     Character        Description
  187.     //  =========    ===========
  188.     //        0        always "*"
  189.     //        1        0 = voice telephone ordering is selectable by the user
  190.     //                1 = voice telephone ordering is disabled
  191.     //        2        0 = no long product descriptions are avaiable
  192.     //                1 = long product descriptions are avaiable starting at
  193.     //                    TEXT resource 3040 (description of catalogItem[0])...
  194.     //        3        1 = Email address required of user.
  195.     //                0 = No it's not.
  196.     //        4        1 = User can change quantity of things on purchase list, but cannot remove item 
  197.     //                    completely from shopping list. This is useful only in very rare cases when you preselect
  198.     //                    the users shopping list for him and need to ensure he doesn't try to "cheat" by getting
  199.     //                    a package deal price for the wrong items.
  200.     //                0 = No such restriction. User can change quantity of things on shopping list, assuming it's
  201.     //                    within minPurchase and maxPurchase parameters you set.
  202.  
  203.     strcpy(myDMCB->privateKey, "*00000000");
  204.  
  205.     
  206.     // Your Product Catalog
  207.  
  208.     // Product names are generally short.
  209.     // If you want to include "long" product descriptions, use the software switch
  210.     // below and be sure to include one TEXT resource for every product in your
  211.     // catalog, starting with resource #3040 (for catalogItem[0]). See the Manual
  212.     // Addendum for more info.    
  213.  
  214.     myDMCB->numItemsInCatalog = 3;
  215.  
  216.      // first, initialize pointers for each item in catalog
  217.      // notice first item is item #0
  218.  
  219.      for (i= 0; i <= myDMCB->numItemsInCatalog - 1; i++)      {
  220.          myDMCB->catalogItem[i] = (catalogItemType *)NewPtr(sizeof(catalogItemType));
  221.      }
  222.      
  223.  
  224.      strcpy(myDMCB->catalogItem[0]->name, "Register Testbed 1.0");
  225.     // registration items (if the item to be bought is the actual program in
  226.     // which AutoPay is embedded), have product codes that start with 9
  227.     strcpy(myDMCB->catalogItem[0]->prodCode, "90001"); 
  228.     myDMCB->catalogItem[0]->minPurchase = 1;
  229.     myDMCB->catalogItem[0]->maxPurchase = 1;
  230.     myDMCB->catalogItem[0]->price = 15.50;
  231.     myDMCB->catalogItem[0]->numVarCodes = 0;
  232.     // Notice how you have to set shipping and handling charges (both domestic
  233.     // and foreign) even if they're zero!
  234.     myDMCB->catalogItem[0]->shUSA = 0;
  235.     myDMCB->catalogItem[0]->shFOR = 0;
  236.     
  237.      strcpy(myDMCB->catalogItem[1]->name, "AutoPay tee-shirt");
  238.     // physical goods that need to be delivered to the purchaser have product codes
  239.     // that start with 7
  240.     strcpy(myDMCB->catalogItem[1]->prodCode, "70002");
  241.     myDMCB->catalogItem[1]->minPurchase = 1;
  242.     myDMCB->catalogItem[1]->maxPurchase = 10;
  243.     myDMCB->catalogItem[1]->price = 20;
  244.     myDMCB->catalogItem[1]->numVarCodes = 2;
  245.     strcpy(myDMCB->catalogItem[1]->varCode1, "LARGE");
  246.     strcpy(myDMCB->catalogItem[1]->varCode2, "X-LARGE");
  247.     myDMCB->catalogItem[1]->shUSA = 4;
  248.     myDMCB->catalogItem[1]->shFOR = 10.50;
  249.     
  250.     strcpy(myDMCB->catalogItem[2]->name, "Upgrade from Testbed 0.9");
  251.     // software upgrades have product codes that start with 8
  252.     strcpy(myDMCB->catalogItem[2]->prodCode, "80003");
  253.     myDMCB->catalogItem[2]->minPurchase = 1;
  254.     myDMCB->catalogItem[2]->maxPurchase = 1;
  255.     myDMCB->catalogItem[2]->price = 5;
  256.     myDMCB->catalogItem[2]->numVarCodes = 0;
  257.     myDMCB->catalogItem[2]->shUSA = 0;
  258.     myDMCB->catalogItem[2]->shFOR = 0;
  259.     
  260.     
  261.     // preselect items to be purchased
  262.     // explanation: when the AutoPay module takes control of
  263.     // the user interface, what items do you want to be preselected 
  264.     // for purchase by the user? If nothing, then set the following to 0.
  265.  
  266.     myDMCB->numItemsPrepurchased = 1;
  267.     
  268.     // Notice how the data structure of the preselected item precisely matches
  269.     // the associated item in the product catalog. Please be careful and do
  270.     // the same!
  271.     
  272.     // The only exception is the "editable" flag, which can be set to false
  273.     // on a pre-selected item, even if the catalog doesn't match this. This 
  274.     // means the user won't be able to remove or edit this preselected item.
  275.     
  276.     strcpy(myDMCB->prepurchaseItem[0].name, "Register Testbed 1.0");
  277.     strcpy(myDMCB->prepurchaseItem[0].prodCode, "90001");
  278.     strcpy(myDMCB->prepurchaseItem[0].varCode, "");
  279.     myDMCB->prepurchaseItem[0].price = 15.50;
  280.     myDMCB->prepurchaseItem[0].quantity = 1;
  281.     myDMCB->prepurchaseItem[0].editable = 1;            // 1=true; 0=false
  282.     myDMCB->prepurchaseItem[0].minPurchase = 1;
  283.     myDMCB->prepurchaseItem[0].maxPurchase = 1;
  284.     myDMCB->prepurchaseItem[0].shUSA=0;
  285.     myDMCB->prepurchaseItem[0].shFOR=0;
  286.     
  287. }
  288.  
  289. /*
  290.         Function: DoAutoPay
  291.  
  292.         Loads the AutoPay code resource into memory, and executes it. This works
  293.         for PowerPC or 68K apps.
  294.  
  295.         Returns: Nothing
  296. */
  297.  
  298. void DoAutoPay(digiMonBlock *dmiBlock)
  299. {
  300.     UniversalProcPtr    resProcUPP;
  301.     ProcInfoType        theProcInfo;
  302.     Handle                resHandle;
  303.     OSErr                result;
  304.  
  305.     resHandle = GetResource('CODE', 60);
  306.     HLockHi(resHandle);
  307.  
  308. #if defined(powerc) || defined (__powerc) || GENERATINGPOWERPC
  309.  
  310.     /*
  311.         For PowerPC machines: The AutoPay resource is 68K, so if we're
  312.         generating a PowerMac app call it from the Mixed Mode Manager
  313.     */
  314.     theProcInfo = kPascalStackBased | STACK_ROUTINE_PARAMETER
  315.       (1, SIZE_CODE(sizeof(long)));
  316.     resProcUPP = NewRoutineDescriptor((ProcPtr)*resHandle,
  317.       theProcInfo, kM68kISA);
  318.     CallUniversalProc(resProcUPP, theProcInfo, (long)dmiBlock);
  319.     DisposeRoutineDescriptor(resProcUPP);
  320.     
  321. #else
  322.  
  323.     /*
  324.         For 68K machines, call it the old fashioned way
  325.     */    
  326.     ((AutoPayCodePtr) *resHandle) ((long) dmiBlock);
  327.     
  328.  
  329. #endif
  330.  
  331.     HUnlock(resHandle);
  332.     ReleaseResource(resHandle);
  333. }
  334.